package com.zhongxiangspace.controller.admin;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zhongxiangspace.entity.*;
import com.zhongxiangspace.entity.VO.BannerVO;
import com.zhongxiangspace.entity.VO.TradeAreaVO;
import com.zhongxiangspace.result.ExceptionResult;
import com.zhongxiangspace.result.NormalResult;
import com.zhongxiangspace.result.Result;
import com.zhongxiangspace.service.*;
import com.zhongxiangspace.tools.FreeMarkerViewUtil;
import com.zhongxiangspace.tools.ReadProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class HtmlCreateController {

    @Autowired
    private BannerService bannerService;
    @Autowired
    private CountyService countyService;
    @Autowired
    private OfficeBuildingService officeBuildingService;
    @Autowired
    private AdvertService advertService;
    @Autowired
    private HouseService houseService;
    @Autowired
    private StationBranchService stationBranchService;
    @Autowired
    private ArticleService articleService;
    /**
     * 生成首页静态页面
     *
     * @return
     */
    @ResponseBody
    @PostMapping("/htmlCreate/html/ajxaCreate.do")
    public Result ajxaCreate(HttpServletRequest request, String pageType) {
        try {
            FreemarkerHtmlConfiguration freemarkerHtmlConfiguration = new FreemarkerHtmlConfiguration();
            freemarkerHtmlConfiguration.setTemplateName("index.ftl");
            freemarkerHtmlConfiguration.setHtmlPath("/mnt/html/web/index.html");
            freemarkerHtmlConfiguration.setTemplatePath("html/pc");
            Map<String, Object> map = new HashMap<>();
            //banner
            QueryAnalyzer bannerQueryAnalyzer = new QueryAnalyzer();
            bannerQueryAnalyzer.setType("PC");
            bannerQueryAnalyzer.setShowCode(1);
            List<BannerVO> list_banner = this.bannerService.queryBannerVO(bannerQueryAnalyzer);
            map.put("banner", list_banner);
            //热门商圈
            QueryAnalyzer tradeAreaQueryAnalyzer = new QueryAnalyzer();
            tradeAreaQueryAnalyzer.setShowCode(1);
            tradeAreaQueryAnalyzer.setHotCode(1);
            List<TradeAreaVO> tradeAreaVOS = this.countyService.queryTradeAreaVO(tradeAreaQueryAnalyzer);
            map.put("tradeArea", tradeAreaVOS);
            //区域
            QueryAnalyzer countQueryAnalyzer = new QueryAnalyzer();
            countQueryAnalyzer.setShowCode(1);
            countQueryAnalyzer.setType("0");
            List<County> countyList = this.countyService.queryByKeyword(countQueryAnalyzer);
            map.put("county", countyList);
            //不能错过的办公室
            QueryAnalyzer buildingQueryAnalyzer = new QueryAnalyzer();
            buildingQueryAnalyzer.setShowCode(1);
            buildingQueryAnalyzer.setHotCode(1);
            List<Map<String, Object>> officeBuildingsList = this.officeBuildingService.queryOfficeBuilding(buildingQueryAnalyzer);
            map.put("officeBuildings", officeBuildingsList);
            //资讯文章1-选址百科
            QueryAnalyzer articleQueryAnalyzer = new QueryAnalyzer();
            articleQueryAnalyzer.setShowCode(1);
            articleQueryAnalyzer.setHotCode(1);
            articleQueryAnalyzer.setParent_id(19);
            List<Map<String, Object>> article_19_List = this.articleService.queryArticles(articleQueryAnalyzer);
            map.put("article_19_List", article_19_List);
            //资讯文章2-项目推介
            articleQueryAnalyzer.setParent_id(20);
            List<Map<String, Object>> article_20_List = this.articleService.queryArticles(articleQueryAnalyzer);
            map.put("article_20_List", article_20_List);
            //广告
            QueryAnalyzer advertQueryAnalyzer = new QueryAnalyzer();
            advertQueryAnalyzer.setShowCode(1);
            List<Map<String, Object>> advertList = this.advertService.queryAdvert(advertQueryAnalyzer);
            map.put("advertList", advertList);

            freemarkerHtmlConfiguration.setData(map);
            FreeMarkerViewUtil.createHtml(freemarkerHtmlConfiguration);
            return new NormalResult(true);
        } catch (Exception e) {
            return new ExceptionResult(e);
        }
    }
    /**
     * 工位详情页
     * @param request
     * @param pageType
     * @return
     */
    @ResponseBody
    @PostMapping("/htmlCreate/html/ajxaCreateNews.do")
    public Result ajxaCreateNews(HttpServletRequest request, String pageType) {
        try {
            QueryAnalyzer queryAnalyzer=new QueryAnalyzer();
            List<Map<String, Object>> stationBranchList = this.articleService.articlesAll(queryAnalyzer);
            for(int i=0;i<stationBranchList.size();i++){
                ajxaCreatHtmlToolsNews((Integer) stationBranchList.get(i).get("article_id"));
            }
            return new NormalResult(true);
        } catch (Exception e) {
            e.printStackTrace();
            return new ExceptionResult(e);
        }
    }
    public boolean ajxaCreatHtmlToolsNews(Integer id) {
        FreemarkerHtmlConfiguration freemarkerHtmlConfiguration = new FreemarkerHtmlConfiguration();
        freemarkerHtmlConfiguration.setTemplateName("newsdetail.ftl");
        freemarkerHtmlConfiguration.setHtmlPath("/mnt/html/web/details/news"+id+".html");
        freemarkerHtmlConfiguration.setTemplatePath("html/pc");
        Map<String, Object> map = new HashMap<>();
        Articles articles = this.articleService.queryOne(id);
        map.put("articles", articles);//news 内容
        ArticleType articleType = this.articleService.queryTypeOne(articles.getArticle_type_id());
        map.put("articleType", articleType);//文章类型
        //热门楼盘
        QueryAnalyzer buildingQueryAnalyzer = new QueryAnalyzer();
        buildingQueryAnalyzer.setShowCode(1);
        buildingQueryAnalyzer.setHotCode(1);
        List<Map<String, Object>> officeBuildingsList = this.officeBuildingService.queryOfficeBuilding(buildingQueryAnalyzer);
        map.put("officeBuildings", officeBuildingsList);
        //热门文章
        QueryAnalyzer newsQueryAnalyzer = new QueryAnalyzer();
        newsQueryAnalyzer.setShowCode(1);
        newsQueryAnalyzer.setHotCode(1);
        List<Map<String, Object>> articleHotList = this.articleService.articlesAll(newsQueryAnalyzer);
        map.put("articleHotList", articleHotList);
        //最新文章
        QueryAnalyzer newQueryAnalyzer = new QueryAnalyzer();
        newQueryAnalyzer.setShowCode(1);
        PageHelper.startPage(1,20,"articles.modify_time  DESC");
        Page<Map<String, Object>> articlenewList = (Page<Map<String, Object>>) this.articleService.articlesAll(newQueryAnalyzer);
        map.put("articlenewList", articlenewList.getResult());
        //上一篇文章
        Articles articlesUP = this.articleService.queryOneUP(id);
        if(articlesUP!=null){
            map.put("articlesUP", articlesUP);
        }else{
            Articles articles1_up=new Articles();
            articles1_up.setTitle("已经是第一篇文章了");
            articles1_up.setArticle_id(null);
            map.put("articlesUP", articles1_up);
        }
        //下一篇文章
        Articles articlesDOWN  = this.articleService.queryOneDOWN(id);
        if(articlesDOWN!=null){
            map.put("articlesDOWN", articlesDOWN);
        }else{
            Articles articles1_down=new Articles();
            articles1_down.setTitle("已经是最后一篇文章了");
            articles1_down.setArticle_id(null);
            map.put("articlesDOWN", articles1_down);
        }
        freemarkerHtmlConfiguration.setData(map);
        FreeMarkerViewUtil.createHtml(freemarkerHtmlConfiguration);
        return true;
    }


    /**
     * 工位详情页
     * @param request
     * @param pageType
     * @return
     */
    @ResponseBody
    @PostMapping("/htmlCreate/html/ajxaCreateStation.do")
    public Result ajxaCreateStation(HttpServletRequest request, String pageType) {
        try {
         QueryAnalyzer queryAnalyzer=new QueryAnalyzer();
         List<Map<String, Object>> stationBranchList = this.stationBranchService.queryAll(queryAnalyzer);
         for(int i=0;i<stationBranchList.size();i++){
              ajxaCreatHtmlToolsStation((Integer) stationBranchList.get(i).get("station_id"));
          }
            return new NormalResult(true);
        } catch (Exception e) {
            return new ExceptionResult(e);
        }
    }

    public boolean ajxaCreatHtmlToolsStation(Integer id) {
        FreemarkerHtmlConfiguration freemarkerHtmlConfiguration = new FreemarkerHtmlConfiguration();
        freemarkerHtmlConfiguration.setTemplateName("projectdetail.ftl");
        freemarkerHtmlConfiguration.setHtmlPath("/html/web/details/s"+id+".html");
        freemarkerHtmlConfiguration.setTemplatePath("html/pc");
        Map<String, Object> map = new HashMap<>();

        StationBranch stationBranch = this.stationBranchService.queryOne(id);
        map.put("stationBranch", stationBranch);
        County county = this.countyService.queryOne(stationBranch.getCounty_id());
        map.put("county", county);
        County trade_area = this.countyService.queryOne(stationBranch.getTrade_area_id());
        map.put("trade_area", trade_area);
        OfficeBuildings office = this.officeBuildingService.queryOne(stationBranch.getBuilding_id());
        map.put("office", office);
        BuildingsAttribute buildingsAttribute = this.officeBuildingService.queryAttr(office.getBuilding_id());
        map.put("buildingsAttribute", buildingsAttribute);
        List<StationHouse> stationHouse = this.stationBranchService.queryStationHouse(id);
        String min = "800";
        String mxn = "2000";
        if (stationHouse.size() > 0) {
            min = stationHouse.get(0).getArea();
            mxn = stationHouse.get(stationHouse.size() - 1).getArea();
        }
        map.put("price_area", min + "-" + mxn);
        map.put("stationHouse", stationHouse);
        List<StationIcon> stationIconList = this.stationBranchService.queryIcon(null);
        map.put("stationIconList", stationIconList);
        //热门楼盘
        QueryAnalyzer buildingQueryAnalyzer = new QueryAnalyzer();
        buildingQueryAnalyzer.setShowCode(1);
        buildingQueryAnalyzer.setHotCode(1);
        List<Map<String, Object>> officeBuildingsList = this.officeBuildingService.queryOfficeBuilding(buildingQueryAnalyzer);
        map.put("officeBuildings", officeBuildingsList);
        freemarkerHtmlConfiguration.setData(map);
        FreeMarkerViewUtil.createHtml(freemarkerHtmlConfiguration);
        return true;
    }

    /**
     * 房源详情页
     * @param request
     * @param pageType
     * @return
     */
    @ResponseBody
    @PostMapping("/htmlCreate/html/ajxaCreateHouse.do")
    public Result ajxaCreateHouse(HttpServletRequest request, String pageType) {
        try {
            QueryAnalyzer queryAnalyzer=new QueryAnalyzer();
            List<Map<String,Object>> maps=this.houseService.queryAll(queryAnalyzer);
            for(int i=0;i<maps.size();i++){
                ajxaCreatHtmlTools((Integer) maps.get(i).get("houes_id"));
            }
            return new NormalResult(true);
        } catch (Exception e) {
            return new ExceptionResult(e);
        }
    }


    public  boolean ajxaCreatHtmlTools(Integer id) {
        FreemarkerHtmlConfiguration freemarkerHtmlConfiguration = new FreemarkerHtmlConfiguration();
        freemarkerHtmlConfiguration.setTemplateName("housedetail.ftl");
        freemarkerHtmlConfiguration.setHtmlPath("/html/web/details/h"+id+".html");
        freemarkerHtmlConfiguration.setTemplatePath("html/pc");
        Map<String, Object> map = new HashMap<>();
        House house = this.houseService.queryOne(id);
        map.put("house", house);
        BuildingsAttribute buildingsAttribute = this.officeBuildingService.queryAttr(house.getBuilding_id());
        map.put("buildingsAttribute", buildingsAttribute);
        County county = this.countyService.queryOne(house.getCounty_id());
        map.put("county", county);
        County trade_area = this.countyService.queryOne(house.getTrade_area_id());
        map.put("trade_area", trade_area);
        OfficeBuildings office = this.officeBuildingService.queryOne(house.getBuilding_id());
        map.put("office", office);
        //热门楼盘
        QueryAnalyzer buildingQueryAnalyzer = new QueryAnalyzer();
        buildingQueryAnalyzer.setShowCode(1);
        buildingQueryAnalyzer.setHotCode(1);
        List<Map<String,Object>> officeBuildingsList=this.officeBuildingService.queryOfficeBuilding(buildingQueryAnalyzer);
        map.put("officeBuildings", officeBuildingsList);
        freemarkerHtmlConfiguration.setData(map);
        FreeMarkerViewUtil.createHtml(freemarkerHtmlConfiguration);
        return true;
    }

    @GetMapping("/htmlCreate/html/create.do")
    public String viemHtml() {
        return "basics/pageProducer/page_html_list";
    }
}
